package com.chen.blue.converter.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * Created By CLT On 2023/12/3.
 */
@SuppressWarnings("all")
@Slf4j
public class DictConverter implements Converter<Object> {

    /**
     * 在读取指定单元格时候触发
     *
     * @param context
     * @return
     * @throws Exception
     */
    @Override
    public Object convertToJavaData(ReadConverterContext<?> context) throws Exception {

        if (context.getReadCellData().getStringValue() == null) {
            return null;
        }
        if (context.getReadCellData().getStringValue().equals("成功")) {
            return 1;
        } else {
            return 0;
        }

    }

    /**
     * 在读取指定单元格时候触发
     *
     * @param context
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Object> context) throws Exception {

        if (context.getValue() == null) {
            return new WriteCellData<>("");
        }

        if ((Integer) context.getValue() == 0) {
            return new WriteCellData<>("失败");
        } else {
            return new WriteCellData<>("成功");
        }

    }


}
